/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CSP;

import java.io.File;

/**
 *
 * @author 3mara
 */
public class Test {

    static int width = 500, height = 500, number_of_orientations;
    static double thetahat, alphahat, sigmaalpha, sigmatheta;
    static double[] zooms = {0.25, 0.166667, 0.111111111111, 0.083333333333333};
    static double nearZRadius = 0.5;

    /**
     * @param args the command line arguments
     */
    public static double calRelevance_1(int zoom, int orient) {
        int intial_position = Test.number_of_orientations / 2;
        double result = Math.pow((zoom - thetahat), 2) / (Math.pow(sigmatheta, 2) * -2);
//        result += Math.pow((orient - alphahat), 2) / (Math.pow(sigmaalpha, 2) * -2);
//        result += Math.exp(-1);
        return Math.exp(result);
    }

    public static double calRelevance_2(int zoom, int orient) {
        return 1;
    }

    public static void main(String[] args) {
//        int numpers[] = { 15, 25};
//        int mindist[] = {7,12}
        int numpers[] = {10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100};
        int mindist[] = {5, 7, 8, 12, 12, 17, 20, 24, 30, 34, 37, 40};
        for (int k = 0; k < 5; k++) {
            int numPersons = numpers[k];
            int minDistance = mindist[k];
            File folder = new File("assets/Scenes/maps_" + numPersons + "_" + minDistance);
            File[] listOfFiles = folder.listFiles();
            listOfFiles = folder.listFiles();
            double cov = 0;
            double avgCov = 0;
            double avgPan = 0;
            double avgRatio = 0;
            double avgTime = 0;
            double camPersonAvg = 0;
            double k_angleAvg = 0;
            for (int mapIndex = 1; mapIndex < listOfFiles.length; mapIndex++) {
                AngularSweep as = new AngularSweep(listOfFiles[mapIndex].getAbsolutePath());
                long start = System.currentTimeMillis();
                as.sweep();
                thetahat = (0 + 3) / 2.0;
                alphahat = (0 + as.number_of_orientations) / 2.0;
                sigmaalpha = (as.number_of_orientations - 0) / 6.0;
                sigmatheta = (3 - 0) / 6.0;
                boolean[] pers = new boolean[as.number_of_men];
                double[] ratios = new double[as.number_of_men];
                double[] cameraPperson = new double[as.number_of_men];
                double[][] diffCamera = new double[as.number_of_men][3];
                int[] camAssign = new int[as.number_of_men];
                number_of_orientations = as.number_of_orientations;
                int[] orientAssign = new int[as.number_of_cameras];
                int[] deltaPan = new int[as.number_of_cameras];
                int[] zoomAssign = new int[as.number_of_cameras];
                for (int camera = 0; camera < as.spotted.length; camera++) {
                    double relevance = 0;
                    for (int zoom = 0; zoom < as.spotted[camera].length; zoom++) {
                        for (int orient = 0; orient < as.spotted[camera][zoom].length; orient++) {
                            for (int per = 0; per < as.spotted[camera][zoom][orient].length; per++) {
                                if (as.spotted[camera][zoom][orient][per]) {
                                    if (calRelevance_1(zoom, orient) > relevance) {
                                        relevance = calRelevance_1(zoom, orient);
                                        orientAssign[camera] = orient;
                                        zoomAssign[camera] = zoom;
                                    }
                                }
                            }
                        }
                    }
                }


//                for (int i = 0; i < orientAssign.length; i++) {
//                    deltaPan[i] = Math.abs(as.number_of_orientations / 2 - orientAssign[i]);
//                }

//                for (int i = 0; i < orientAssign.length; i++) {
//                    for (int per = 0; per < as.spotted[i][zoomAssign[i]][orientAssign[i]].length; per++) {
//                        if (as.spotted[i][zoomAssign[i]][orientAssign[i]][per]) {
//                            cameraPperson[per]++;
//                            double angle = Geometry.computeAngle(
//                                    new double[]{as.people.get(per).x, as.people.get(per).y, 0},
//                                    new double[]{as.cameras.get(i).x, as.cameras.get(i).y, 0},
//                                    new double[]{640, as.people.get(per).y, 0});
//                            angle = Math.ceil(angle * 360 / Math.PI);
//                            if (angle > 120) {
//                                if (diffCamera[per][0] != 1) {
//                                    diffCamera[per][0] = 1;
//                                }
//                            } else if (angle > 240) {
//                                if (diffCamera[per][0] != 1) {
//                                    diffCamera[per][1] = 1;
//                                }
//                            } else {
//                                if (diffCamera[per][0] != 1) {
//                                    diffCamera[per][2] = 1;
//                                }
//                            }
//                        }
//                    }
//                }
//
//                double cameraPersonAvg = 0;
//                for (int i = 0; i < cameraPperson.length; i++) {
//                    cameraPersonAvg += cameraPperson[i];
//                }
//                cameraPersonAvg /= cameraPperson.length;
//                camPersonAvg += cameraPersonAvg;

//                for (int i = 0; i < orientAssign.length; i++) {
//                    for (int per = 0; per < as.spotted[i][zoomAssign[i]][orientAssign[i]].length; per++) {
//                        if (as.spotted[i][zoomAssign[i]][orientAssign[i]][per] && !pers[per]) {
//                            cov++;
//                            pers[per] = true;
//                            double ratio = 0;
//                            ratio = nearZRadius * 10 / as.cameras.get(i).distance(as.people.get(per));
//                            double totalnearZ = Test.zooms[zoomAssign[i]] * Math.PI * nearZRadius;
//                            ratio /= totalnearZ;
//                            ratios[per] = ratio;
//                        }
//                    }
//                }

//                double result = 0;
//                for (int i = 0; i < diffCamera.length; i++) {
//                    for (int j = 0; j < diffCamera[i].length; j++) {
//                        result += diffCamera[i][j];
//                    }
//                }
//                result /= diffCamera[0].length;
//                result /= as.number_of_men;
//                k_angleAvg += result;

//                double ratioAvg = 0;
//                for (int i = 0; i < ratios.length; i++) {
//                    ratioAvg += ratios[i];
//                }
//                ratioAvg /= ratios.length;
//                avgRatio += ratioAvg;
//                cov /= as.number_of_men;
//                avgCov += cov;

//                double panAvg = 0;
//                for (int i = 0; i < deltaPan.length; i++) {
//                    double temp = deltaPan[i] * (360 / as.number_of_orientations);
//                    while (temp < 0) {
//                        temp += 360;
//                    }
//                    panAvg += temp;
//                }
//                panAvg /= deltaPan.length;
//                avgPan += panAvg;
//
                long end = System.currentTimeMillis();
                avgTime += end - start;

            }
//            System.out.println(avgCov / listOfFiles.length + "\t");
//            System.out.println(avgRatio / listOfFiles.length + "\t");
//            System.out.println(avgPan / listOfFiles.length + "\t");
            System.out.println(avgTime / listOfFiles.length);
//            System.out.println(camPersonAvg / listOfFiles.length + "\t");
//            System.out.println(k_angleAvg / listOfFiles.length + "\t");
        }
    }
}
